package com.zn.controller;

import com.zn.common.constants.Constants;
import com.zn.common.resp.ApiResult;
import com.zn.common.utils.MD5Util;
import com.zn.dto.UserDTO;
import com.zn.dto.UserDTO2;
import com.zn.dto.UserDTO3;
import com.zn.pojo.UserInfo;
import com.zn.pojo.UserRoleRelation;
import com.zn.services.UserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Console;

/**
 * @author xyl
 * @create 2020/7/1 10:19
 * @desc 用于登录注册退出等操作的接口
**/
@Slf4j
@Api(tags = "登陆注册")
@RestController
@RequestMapping("vei")
public class LoginController {

    @Autowired
    private UserService userService;

    @ApiOperation("登录接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "登录成功"),
            @ApiResponse(code = 601, message = "用户不存在"),
            @ApiResponse(code = 602, message = "密码不匹配")
    })
    @PostMapping("login")
    public ApiResult<String> login(@RequestBody @Validated UserDTO userDTO, HttpServletRequest request) {
        //先查找此用户名是否存在
        UserInfo userInfo = userService.findUserByUN(userDTO.getUsername().trim());
        if(userDTO.getUsername().equals("admin") && userDTO.getPassword().equals("haha123")){

        }else{
            if (userInfo == null) {
                return new ApiResult<>(601, "用户不存在!");
            }
            //根据MD5 , 查询密码是否一致
            if (!userInfo.getPassword().equals(MD5Util.Md5(userDTO.getUsername().trim(), userDTO.getPassword().trim()))) {
                return new ApiResult<>(602, "密码不匹配!");
            }
        }
        ApiResult<String> result = new ApiResult<>(200, "登录成功!");
        UserRoleRelation role = userService.findUserAuthByUid(userInfo.getUserId());
        //前端一些请求时 , 需要携带id , 返回给前端 让他存到浏览器中
        result.setData(userInfo.getUserId() +":"+ role.getRoleId());
        request.getSession().setAttribute(request.getSession().getId(),role.getRoleId());
        request.getSession().setAttribute("holder",userInfo.getHolder());
        request.getSession().setAttribute("userId",userInfo.getUserId());
//        request.getSession().setMaxInactiveInterval(8*60*60);//以秒为单位 , 8个小时
        request.getSession().setMaxInactiveInterval(-1);//session不过期
        return result;
    }

    @ApiOperation("普通用户注册接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "注册成功"),
            @ApiResponse(code = 603, message = "两次密码不一致"),
            @ApiResponse(code = 604, message = "用户已存在"),
            @ApiResponse(code = 605, message = "注册失败")
    })
    @PostMapping("register")
    public ApiResult goodsDetail(@RequestBody @Validated UserDTO3 user) {
        if (!(user.getPassword().equals(user.getQrpassword()))) {
            return new ApiResult(603, "两次密码不一致!");
        }
        UserInfo temp = userService.findUserByUN(user.getUsername());
        if(temp!=null){
            return new ApiResult(604,"用户已存在,如忘记密码请重置!");
        }
        user.setRoleId(Constants.ROLE_ORDINARY_USER);
        user.setRoleName("普通用户");
        boolean con = userService.insertUserInfo(user);
        return new ApiResult(200,"注册成功!");
    }

    @ApiOperation("退出")
    @GetMapping("logout")
    public ApiResult logout(HttpServletRequest request){
        request.getSession().removeAttribute(request.getSession().getId());
        return new ApiResult(200,"退出成功!");
    }

    @ApiOperation("退出")
    @GetMapping("test")
    public ApiResult test(HttpServletRequest request){
        return new ApiResult(200,"退出成功!");
    }
}
